﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace VlcPlayer
{
    public partial class RemoteDialog : PhoneApplicationPage
    {
        private class MyClass
        {
            public Uri _src;
            public WebClient _webClient;
            public Page _srcPage;  
        }

        private Uri _src;
        private WebClient _webClient;

        public RemoteDialog()
        {
            InitializeComponent();
        }

        public static void ShowIfAvailableAsync(string uri, Page page)
        {
            try
            {
                var webClient = new WebClient();
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                var src = new Uri(uri);
                webClient.DownloadStringAsync(src, new MyClass() { _src = src, _webClient = webClient, _srcPage = page });

            }
            catch (Exception)
            {
            }
        }

        static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    var state = (MyClass)e.UserState;
                    state._srcPage.Dispatcher.BeginInvoke(() =>
                                                              {
                                                                  state._srcPage.NavigationService.Navigate(new Uri("/RemoteDialog.xaml?src=" + state._src, UriKind.Relative));
                                                              });
                    
                }

            }
            catch (Exception)
            {            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            try
            {
                var src = NavigationContext.QueryString["src"];
                _src = new Uri(src);


                _webClient = new WebClient();
                _webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(_webClient_DownloadStringCompleted);
                _webClient.DownloadStringAsync(_src);

            }
            catch (Exception)
            {
                NavigationService.GoBack();
            }
        }

        void _webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                NavigationService.GoBack();
            }
            else
            {
                SetContent(e.Result);

            }
        }

        private void SetContent(string result)
        {
            DownloadStringCompletedEventArgs e;
            try
            {
                var ctrl = XamlReader.Load(result);
                Inner.Content = ctrl;
            }
            catch (Exception)
            {
                NavigationService.GoBack();
            }
        }
    }
}